﻿#region Apache License
/* 
   Copyright 2011 Jorge Teixeira

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
#endregion

using System;

namespace AdHoc.Wavesque
{
    /// <summary>
    /// Extension metods for type Esque.
    /// </summary>
    public static class EsqueExtensions
    {
        /// <summary>
        /// Calculates a given number of points of a waveform for a given initial position and simulates
        /// half wave rectification. The positive half of the wave is passed, while the other half is blocked. 
        /// </summary>
        /// <param name="source">The object representation of a waveform.</param>
        /// <param name="numberPoints">Number of points to compute.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        /// <param name="x0">Initial position.</param>
        public static void ToHalfWaveRectification(this Esque source, uint numberPoints, out double[] x, out double[] y, double x0)
        {
            source.GetValues(numberPoints, out x, out y, x0);
            ToHalfWaveRectification(y);
        }

        /// <summary>
        /// Calculates a given number of points of a waveform for a given initial position and simulates
        /// half wave rectification. The positive half of the wave is passed, while the other half is blocked. 
        /// </summary>
        /// <param name="source">The object representation of a waveform.</param>
        /// <param name="numberPoints">Number of points to compute.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        public static void ToHalfWaveRectification(this Esque source, uint numberPoints, out double[] x, out double[] y)
        {
            source.GetValues(numberPoints, out x, out y, 0.0);
            ToHalfWaveRectification(y);
        }

        /// <summary>
        /// Calculates a default number of points of a waveform for a given initial position and simulates
        /// half wave rectification. The positive half of the wave is passed, while the other half is blocked. 
        /// </summary>
        /// <param name="source">The object representation of a waveform.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        /// <param name="x0">Initial position.</param>
        public static void ToHalfWaveRectification(this Esque source, out double[] x, out double[] y, double x0)
        {
            source.GetValues(out x, out y, x0);
            ToHalfWaveRectification(y);
        }

        /// <summary>
        /// Calculates a default number of points of a waveform for a given initial position and simulates
        /// half wave rectification. The positive half of the wave is passed, while the other half is blocked. 
        /// </summary>
        /// <param name="source">The object representation of a waveform.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        public static void ToHalfWaveRectification(this Esque source, out double[] x, out double[] y)
        {
            source.GetValues(out x, out y, 0.0);
            ToHalfWaveRectification(y);
        }

        /// <summary>
        /// Given a set of ordinates of a waveform, simulates half wave rectification. 
        /// The positive half of the wave is passed, while the other half is blocked. 
        /// </summary>
        /// <param name="y">Ordinates.</param>
        private static void ToHalfWaveRectification(double[] y)
        {
            for (int i = 0; i < y.Length; i++)
            {
                switch (Math.Sign(y[i]))
                {
                    default:
                    case 0:
                    case 1:
                        break;

                    case -1:
                        y[i] = 0.0;
                        break;
                }
            }
        }

        /// <summary>
        /// Calculates a given number of points of a waveform for a given initial position and simulates
        /// full wave rectification. The positive half of the wave is passed, the negative half of the 
        /// wave is converted to positive. 
        /// </summary>
        /// <param name="source">The object representation of a waveform.</param>
        /// <param name="numberPoints">Number of points to compute.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        /// <param name="x0">Initial position.</param>
        public static void ToFullWaveRectification(this Esque source, uint numberPoints, out double[] x, out double[] y, double x0)
        {
            source.GetValues(numberPoints, out x, out y, x0);
            ToFullWaveRectification(y);
        }

        /// <summary>
        /// Calculates a given number of points of a waveform for a given initial position and simulates
        /// full wave rectification. The positive half of the wave is passed, the negative half of the 
        /// wave is converted to positive. 
        /// </summary>
        /// <param name="source">The object representation of a waveform.</param>
        /// <param name="numberPoints">Number of points to compute.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        public static void ToFullWaveRectification(this Esque source, uint numberPoints, out double[] x, out double[] y)
        {
            source.GetValues(numberPoints, out x, out y, 0.0);
            ToFullWaveRectification(y);
        }

        /// <summary>
        /// Calculates a default number of points of a waveform for a given initial position and simulates
        /// full wave rectification. The positive half of the wave is passed, the negative half of the 
        /// wave is converted to positive. 
        /// </summary>
        /// <param name="source">The object representation of a waveform.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        /// <param name="x0">Initial position.</param>
        public static void ToFullWaveRectification(this Esque source, out double[] x, out double[] y, double x0)
        {
            source.GetValues(out x, out y, x0);
            ToFullWaveRectification(y);
        }

        /// <summary>
        /// Calculates a default number of points of a waveform for a given initial position and simulates
        /// full wave rectification. The positive half of the wave is passed, the negative half of the 
        /// wave is converted to positive. 
        /// </summary>
        /// <param name="source">The object representation of a waveform.</param>
        /// <param name="x">Abscissae.</param>
        /// <param name="y">Ordinates.</param>
        public static void ToFullWaveRectification(this Esque source, out double[] x, out double[] y)
        {
            source.GetValues(out x, out y, 0.0);
            ToFullWaveRectification(y);
        }

        /// <summary>
        /// Given a set of ordinates of a waveform, simulates full wave rectification. 
        /// The positive half of the wave is passed, the negative half of the wave is converted to positive. 
        /// </summary>
        /// <param name="y">Ordinates.</param>
        private static void ToFullWaveRectification(double[] y)
        {
            for (int i = 0; i < y.Length; i++)
            {
                switch (Math.Sign(y[i]))
                {
                    default:
                    case 0:
                    case 1:
                        break;

                    case -1:
                        y[i] = -1 * y[i];
                        break;
                }
            }
        }
    }
}
